package com.yonyou.pmclouds.plan.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertMultiProcessor;
import com.yonyou.pmclouds.basecom.bp.template.MultiInsertBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.measureindex.entity.MeasureIndexHistoryVO;
import com.yonyou.pmclouds.measureindex.service.rmiitf.MeasureIndexHisMaintin;
import com.yonyou.pmclouds.picture.entity.PictureVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.plan.entity.PlanProgressHistoryVO;
import com.yonyou.pmclouds.plan.entity.PlanProgressVO;
import com.yonyou.pmclouds.plan.entity.ScheduleReportVO;
import com.yonyou.pmclouds.plan.mapper.PlanProgressHistoryMapper;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressHistoryQueryService;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleProgressHistoryService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = IScheduleProgressHistoryService.class)
public class ScheduleProgressHistoryServiceImpl implements IScheduleProgressHistoryService {

    @Autowired
    private PlanProgressHistoryMapper historyMapper;

    @Autowired
    private IScheduleProgressHistoryQueryService historyQueryService;

    @Autowired
    private MeasureIndexHisMaintin indexHisMaintin;

    @Autowired
    private PictureRemoteService picService;

    @Override
    public PlanProgressHistoryVO[] insert(PlanProgressHistoryVO[] historyVOS) throws BusinessException {
        return insert(historyVOS, true);
    }

    @Override
    public PlanProgressHistoryVO[] insert(PlanProgressHistoryVO[] historyVOS, boolean isGenId) throws BusinessException {
        if (historyVOS == null || historyVOS.length <= 0) {
            return null;
        }

        MultiInsertBpTemplate<PlanProgressHistoryVO> insertBpTemplate = new MultiInsertBpTemplate<>(historyMapper, isGenId);
        insertBpTemplate.addBeforeProcessor(new AuditInfoInsertMultiProcessor<PlanProgressHistoryVO>());
        insertBpTemplate.insert(historyVOS);

        return historyVOS;
    }

    @Override
    public ScheduleReportVO updateProgress(ScheduleReportVO reportVO) throws BusinessException {
        if (reportVO == null || reportVO.getPlanProgressVO() == null) {
            return null;
        }

        PlanProgressVO progressVO = reportVO.getPlanProgressVO();
        PlanProgressHistoryVO historyVO = new PlanProgressHistoryVO();
        historyVO.setPkPlan(progressVO.getPkPlan());
        historyVO.setProgress(progressVO.getProgress());
        historyVO.setDescription(progressVO.getDescription());
        historyVO.setStartDate(progressVO.getStartDate());
        historyVO.setEndDate(progressVO.getEndDate());
        historyVO.setBillType(BillTypeConst.PROCESS_WRITE_BILLTYPE);
        historyVO.setBillStatus(BillStatusConst.COMMON_FREE_STATUS);
        historyVO.setPkProgressHis(UUID.randomUUID().toString());

        Map<String, List<PlanProgressHistoryVO>> pkPlanWithHistoryMap = historyQueryService.queryByPkPlansWithMap(
                new String[]{reportVO.getPkScheduleItem()});

        List<PlanProgressHistoryVO> historyVOList = pkPlanWithHistoryMap.get(historyVO.getPkPlan());
        int fillCount = historyVOList == null ? 0 : historyVOList.size();
        historyVO.setFillCount(fillCount + 1);

        this.insert(new PlanProgressHistoryVO[]{historyVO}, false);

        // 保存指标填报记录
        insertIndexHistory(reportVO, historyVO);

        // 保存图片
        insertPictures(reportVO, historyVO);

        return reportVO;

    }

    private void insertIndexHistory(ScheduleReportVO reportVO, PlanProgressHistoryVO historyVO) throws BusinessException {
        MeasureIndexHistoryVO[] indexHistoryVOS = reportVO.getIndexHistoryVOS();
        if(indexHistoryVOS == null || indexHistoryVOS.length <= 0){
            return;
        }

        for(MeasureIndexHistoryVO indexHistoryVO : indexHistoryVOS){
            indexHistoryVO.setPkScheduleItem(reportVO.getPkScheduleItem());
            indexHistoryVO.setPkProgressHis(historyVO.getPkProgressHis());
        }

        indexHisMaintin.insert(reportVO.getIndexHistoryVOS());
    }

    private void insertPictures(ScheduleReportVO reportVO, PlanProgressHistoryVO historyVO) throws BusinessException{
        if(reportVO.getPictures() == null || reportVO.getPictures().length <= 0){
            return;
        }

        String[] pictures = reportVO.getPictures();

        List<PictureVO> insertPictureList = new ArrayList<>();
        for(String picture : pictures){
            PictureVO pictureVO = new PictureVO();
            pictureVO.setPicture_name(picture);
            pictureVO.setPk_entity(historyVO.getPkProgressHis());
            pictureVO.setPk_bill(reportVO.getPkScheduleItem());
            pictureVO.setPk_project(reportVO.getPkProject());
            pictureVO.setBill_status(historyVO.getBillStatus());
            pictureVO.setSrc_bill_type(BillTypeConst.PROCESS_WRITE_BILLTYPE);
            insertPictureList.add(pictureVO);
        }

        if(insertPictureList.size() > 0){
            picService.insertPictures(insertPictureList.toArray(new PictureVO[]{}));
        }
    }
}
